package com.sjm.bookshop.controller.client;

import com.sjm.bookshop.entity.Cart;
import com.sjm.bookshop.entity.UCart;
import com.sjm.bookshop.service.client.ClientService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartCtrl {
    @Autowired
    private ClientService clientService;

    //  添加到购物车
    @RequestMapping("/add")
    public Result addCart(@RequestBody Cart cart) {
        System.out.println(cart);
        if (cart.getU_id() > 0 && cart.getCartNum() > 0 && cart.getB_id() > 0) {
            int add = clientService.add(cart);
            if (add > 0) {
                return Result.success(200, "已添加到购物车");
            } else {
                return Result.fail(201, "重复添加");
            }
        } else {
            return Result.fail(202, "错误请求");
        }
    }

    //    查询用户购物车
    @RequestMapping("/getUserCart/{u_id}")
    public Result getCart(@PathVariable("u_id") int u_id) {
        List<UCart> cart = clientService.getCart(u_id);
        if (cart.size() > 0) {
            return Result.success(200, cart, "");
        } else if (cart.size() == 0) {
            return Result.fail(201, "你的购物车空空的,去添加商品吧");
        } else {
            return Result.fail(202, "请求错误");
        }
    }

    //    修改购物车的数量
    @RequestMapping("/updateCart")
    public Result updateCart(@RequestBody Cart cart) {
        int i = clientService.update(cart);
        if (i > 0) {
            return Result.success(200, cart, "success");
        } else {
            return Result.fail(202, "网络错误");
        }
    }

    //    删除
    @RequestMapping("/deleteCart/{u_Id}/{b_Id}")
    public Result deleteCart(@PathVariable("u_Id") int u_Id, @PathVariable("b_Id") int b_Id) {


        int i = clientService.delete(u_Id, b_Id);
        if (i > 0) {
            return Result.success(200, "商品已删除");
        } else {
            return Result.fail(202, "网络错误");
        }
    }

    //    批量删除
    @RequestMapping("/deleteList")
    public Result deleteCartList(@RequestBody List<Integer> list) {
//       接收JSON格式的list数组，并转换为数组
        if (list.size() == 0){
            return Result.fail(202,  "错误请求");
        }
        int i = clientService.deleteBatch(list);
        if (i>0){
            return Result.success(200, "商品已删除");
        }else {
            return Result.fail(201,  "网络错误！请稍后从试");
        }

    }

//    提交订单
    @RequestMapping("/addOrder")
    public Result addOrder(@RequestBody Map<String,Object> map) throws ParseException {
        int res = clientService.addOrder(map);
        if (res > 0){
            return Result.success(200,"添加订单成功,请在30分钟内完成支付");
        }else{
            return Result.fail(201,"购买失败");
        }
    }

    /**
     * 单独购买
     */
    @RequestMapping("/addOne")
    public Result addOneOrder(@RequestBody Map<String,Object> map) throws ParseException {

        int res = clientService.addOne(map);
        if (res > 0){
        System.out.println(map);
            return Result.success(200,"购买成功");
        }else{
            return Result.fail(201,"购买失败");
        }

    }

}
